package com.xiumu.aspect;

import cn.hutool.json.JSONUtil;
import com.xiumu.common.core.utils.StopWatchUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;


/**
 * 日志切面，记录用户的请求路径与参数
 * 返回数据先不打印
 *
 * @author XiuMu
 * @Date 2022-11-30 16:53
 **/
@Slf4j
@Aspect
@Component
public class RequestAspect {

	@Autowired
	private HttpServletRequest request;


	/**
	 * 打印请求日志
	 */
	@Before(value = "execution(public * com.xiumu.controller.*.*.*(..))")
	public void before(JoinPoint point) {
		StopWatchUtil.start();
		log.info("url: {}, args: {}", request.getRequestURI(), JSONUtil.toJsonStr(point.getArgs()));
	}

	/**
	 * 打印请求日志
	 */
	@AfterReturning(value = "execution(public * com.xiumu.controller.*.*.*(..))")
	public void after(JoinPoint point) {
		log.info("url: {}, 执行耗时，{}ms", request.getRequestURI(), StopWatchUtil.getGlobalTimeAndStop());
	}

}
